# -*- coding: utf-8 -*-
#
###
#
# Copyright (C) 2009 yed_ <yed (a) vanyli (o) net>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
#
###


import os
from pyfuploader.core import PyfuploadCore
from gettext import gettext as _

class PyfuploadCLI(PyfuploadCore):

  _params = []

  def __init__(self, params):
    PyfuploadCore.__init__(self)

    self._init_params(params)

    self.do_upload()

  def do_upload(self):
    self.before_upload()
    self.upload()
    self.post_upload()

  def _init_params(self, params):
    par_len = len(params)

    self.set_source(params[0])

    if (par_len == 1):  # got only file name
      self.set_default()

    elif (par_len == 2): # got file name and new file name
      self.set_default()

    elif (par_len == 3): # file name, upload connection and directory
      self.set_connection(params[1])
      self.set_directory(params[2])

    elif (par_len == 4): # file name, new file name,  upload connection and directory
      self.set_connection(params[2])
      self.set_directory(params[3])

    self.set_target()

  def debug_output(self, lvl, msg):
    if lvl == 1:
      print msg

  '''@todo - rework '''
  def set_default(self):
    for one in self.connections:
      if self.default_connection == one:
        self.set_connection(one)

        for conn_dir in self.conn_dirs[one].iteritems():
          if (not self.get_directory()):
            self.set_directory(conn_dir[0])
            continue
          if (conn_dir[1]['default']):
            self.set_directory(conn_dir[0])
            break

        break

  def post_upload(self):
    try:
      import gtk, pygtk
      clipboard = gtk.clipboard_get()
      clipboard.set_text(self.target['url'])
      clipboard.store()
    except ImportError:
      print( _("Unable to copy URI to clipboard"))
      return False
    finally:
      print( _("Target URI: ") + self.target['url'])

